//
//  ZZNewCustomerAnalysisVC.m
//  新项目
//
//  Created by liGordon on 17/3/13.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZNewCustomerAnalysisVC.h"
#import "ZZNewCustomerTableViewCell.h"
#import "ZZCustomViewModel.h"
#import "ZZNewCustomerContentModel.h"
#import "ZZNewCustomerModel.h"
#import "ZZNewCustomerSelectedView.h"
#import "ZZNewCustomerDetailVCViewController.h"
static NSString *identifier = @"NewCustomerTableViewCell";
@interface ZZNewCustomerAnalysisVC ()<UITableViewDataSource,UITableViewDelegate,ZZCustomRevertAnalysisCellDelegate,ZZNewCustomerSelectedViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSString *nowStr;
@property (nonatomic,strong)NSString *beforeStr;
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic, strong)ZZNewCustomerSelectedView *TanCView;
@end

@implementation ZZNewCustomerAnalysisVC

-(ZZNewCustomerSelectedView *)TanCView
{
    if (_TanCView == nil) {
        _TanCView = [ZZNewCustomerSelectedView ViewWithXib];
        _TanCView.delegate = self;
    }
    return _TanCView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"新增客户分析";
    
    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_wxcb_01"] style:UIBarButtonItemStylePlain target:self action:@selector(clickForSeletecd)];
    self.navigationItem.rightBarButtonItem=item;
    self.beforeStr = @"";
    self.nowStr = @"";
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index=1;
        [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];
        [self.menuTable.mj_footer  resetNoMoreData];
    }];
    self.menuTable.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.total>upNum*self.index) {
            self.index++;
            [self initDataWithPageIndex:[NSString stringWithFormat:@"%ld",self.index]];
            
        }
        else
        {
            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
    }];
    
    
    self.menuTable.mj_header=header;
    [self.menuTable.mj_header beginRefreshing];

    [self.view addSubview:self.TanCView];
    self.TanCView.frame=CGRectMake(0, -newheight-64-200, newwidth, newheight);
}
- (void)setupTableView
{
    _menuTable.delegate = self;
    _menuTable.dataSource = self;
    _menuTable.rowHeight = 80;
    [_menuTable registerNib:[UINib nibWithNibName:@"ZZNewCustomerTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
}
-(NSMutableArray *)dataArr
{
    
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZNewCustomerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.model = self.dataArr[indexPath.row];
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}




#pragma mark -数据请求
-(void)initDataWithPageIndex:(NSString *)pageIndex
{
    NSString *PageIndex = [NSString stringWithFormat:@"%ld",(long)pageIndex];
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
   

    
  
    [[ZZCustomViewModel shareViewModel]getNewCustomConvertAnalysisWithCompanyID:loginDict[@"CompanyID"] andPageIndex:pageIndex andPageSize:@(upNum)  andStartTime:self.beforeStr andEndTime:self.nowStr andSuccess:^(NSDictionary *result) {
        if (result) {
            
            [ZZNewCustomerModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"rows" : @"ZZNewCustomerContentModel",
                         };
            }];
            ZZNewCustomerModel *fg = [ZZNewCustomerModel mj_objectWithKeyValues:result[@"resultJson"]];
            
            self.total = [fg.total integerValue];
            
            NSMutableArray *mutableArr=[NSMutableArray array];
            for (NSDictionary *dic in fg.rows) {
                [mutableArr addObject:dic];
            }
            
            
            if (self.index!=1) {
                if (mutableArr.count==0) {
                    [self.menuTable.mj_footer endRefreshingWithNoMoreData];
                    return ;
                }
                [self.dataArr addObjectsFromArray: mutableArr];
                [self.menuTable.mj_footer endRefreshing];
                
            }
            else
            {
                self.dataArr=mutableArr;
                [self.menuTable.mj_header endRefreshing];
                
            }
            
            
            
            
            
            
            
            [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
            self.noDataView.label.text=@"暂无数据";
            
            [self.menuTable reloadData];
            
            
        }
        
        
    } andFailer:^{
        [self.menuTable.mj_header endRefreshing];
        [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
        //点击重新加载的block
        WEAKSELF
        self.noDataView.myBlock=^{
            [weakSelf.menuTable.mj_header beginRefreshing];
        };
        
    }];
    
}


#pragma mark - 跳转详情

-(void)clickDetail:(ZZNewCustomerContentModel *)model
{
    ZZNewCustomerDetailVCViewController *vc = [[ZZNewCustomerDetailVCViewController alloc]init];
    vc.model = model;
    PushVC(vc)
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ZZNewCustomerDetailVCViewController *vc = [[ZZNewCustomerDetailVCViewController alloc]init];
    vc.model = self.dataArr[indexPath.row];
    PushVC(vc)
    
}

//点击查询
-(void)clickForSeletecd
{
    [UIView animateWithDuration:0.25 animations:^{
        self.TanCView.frame=CGRectMake(0, 64, newwidth, newheight-64);
    }];
    
    
    
}

-(void)clickForSeletectedDataWithStime:(NSString *)startTime andEndTime:(NSString *)endTime
{
    [self hiddenTheView];
    self.beforeStr=startTime;
    self.nowStr=endTime;
    [self.menuTable.mj_header beginRefreshing];
}


//关闭
-(void)hiddenTheView
{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.TanCView.frame=CGRectMake(0, -newheight-64-200, newwidth, newheight);
    }];
    
}


@end
